package com.yhp.dao.impl;

import com.yhp.bean.Menu;
import com.yhp.dao.DBUtils;
import com.yhp.dao.MenuDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MenuDaoImpl extends DBUtils implements MenuDao {
    @Override
    public List<Menu> getMenuList() {
        List<Menu> menuList = new ArrayList<>();
        try {
            String sql = "select * from menu";
            resultSet = query(sql, null);
            qu(menuList);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return menuList;
    }

    @Override
    public List<Menu> getMenuList(int pageIndex, int pageSize) {
        List list = new ArrayList();
        List<Menu> menuList = new ArrayList<>();
        try {
            String sql = "select * from menu limit ?,? ";
            list.add((pageIndex-1)*pageSize);
            list.add(pageSize);
            resultSet = query(sql,list);
            qu(menuList);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return menuList;
    }

    @Override
    public int total() {
        int total=0;
        try {
            String sql = "select count(*) from menu";
            resultSet = query(sql,null);
            if (resultSet.next()){
               total = resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return total;
    }

    private void qu(List<Menu> menuList) throws SQLException {
        while (resultSet.next()) {
            Menu menu = new Menu();
            menu.setMenuId(resultSet.getInt("menuid"));
            menu.setMenuName(resultSet.getString("menuname"));
            menu.setUpmenuId(resultSet.getInt("upmenuid"));
            menu.setState(resultSet.getInt("state"));
            menu.setDesc(resultSet.getString("desc"));
            menu.setUrl(resultSet.getString("url"));
            menuList.add(menu);
        }

    }

    @Override
    public List<Menu> getMenuList(int roleId) {
        List<Menu> menuList = new ArrayList<>();
        try {
            String sql = "select * from menu m,middle z where m.menuid=z.menuid and z.roleid=? ";
            List list = new ArrayList();
            list.add(roleId);
            ResultSet resultSet = query(sql, list);
            while (resultSet.next()){
                Menu menu = new Menu();
                menu.setState(resultSet.getInt("state"));
                menu.setUrl(resultSet.getString("url"));
                menu.setUpmenuId(resultSet.getInt("upmenuid"));
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setDesc(resultSet.getString("desc"));
                menuList.add(menu);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
        }
        return menuList;
    }
}